package com.vueblog.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vueblog.service.BlogService;
import com.vueblog.vo.DetailBlog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SearchController {
    private final int pageSize=6;
    @Autowired
    private BlogService blogService;

    @GetMapping("/searchShowBlogs")
    public Object searchShowBlogs(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam String keyword){
        JSONObject jsonObject = new JSONObject();
        //封装展示的Blog信息到PageInfo
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<DetailBlog> blogs = blogService.findShowBlogByTitleOrContent(keyword);
        PageInfo<DetailBlog> pageInfo = new PageInfo<DetailBlog>(blogs);
        jsonObject.put("pageInfo",pageInfo);
        return jsonObject;
    }
}
